package org.fast.develop.logicflow.runtime;

import org.fast.develop.logicflow.builder.LogicFlowBuilderFactory;
import org.fast.develop.logicflow.core.LogicFlowConfiguration;
import org.fast.develop.logicflow.runtime.cache.IThreadStore;
import org.fast.develop.logicflow.runtime.context.LogicFlowContextManager;
import org.fast.develop.logicflow.runtime.convert.LogicFlowConvertConfiguration;
import org.fast.develop.logicflow.runtime.convert.builder.DesignerModelBuilder;
import org.fast.develop.logicflow.runtime.convert.execute.DefinitionsToJsonExecutor;
import org.fast.develop.logicflow.runtime.convert.execute.DesignerBuilderFactory;
import org.fast.develop.logicflow.runtime.convert.execute.DesignerToDefinitionsExecutor;
import org.fast.develop.logicflow.runtime.executor.TaskListenerCreator;

/**
 * @author 莫寅
 * @createTime 2022-11-24 23:21
 * @desc：
 */
public class LogicFlowRuntimeConfiguration {

    private LogicFlowExecutor logicFlowExecutor;

    private LogicFlowBuilderFactory logicFlowBuilderFactory;

    private LogicFlowConfiguration logicFlowConfiguration;

    private LogicFlowConvertConfiguration logicFlowConvertConfiguration;

    private LogicRuntimeService logicRuntimeService;

    private LogicFlowContextManager logicFlowContextManager;


    public LogicFlowRuntimeConfiguration(TaskListenerCreator taskListenerCreator, LogicFlowConfiguration logicFlowConfiguration, IThreadStore threadStore) {
        this.logicFlowContextManager = new LogicFlowContextManager();

        this.logicFlowConfiguration = logicFlowConfiguration;

        this.logicFlowExecutor = new LogicFlowExecutor(logicFlowConfiguration.getElementFactory(), taskListenerCreator, logicFlowContextManager, threadStore);
        this.logicFlowBuilderFactory = new LogicFlowBuilderFactory(this.getLogicFlowConfiguration());

        this.logicFlowConvertConfiguration = logicFlowConvertConfiguration();

    }


    public LogicFlowConvertConfiguration logicFlowConvertConfiguration() {
        DesignerBuilderFactory designerBuilderFactory = new DesignerBuilderFactory();
        DesignerToDefinitionsExecutor designerToDefinitionsExecutor = new DesignerToDefinitionsExecutor(designerBuilderFactory);
        DefinitionsToJsonExecutor definitionsToJsonExecutor = new DefinitionsToJsonExecutor(logicFlowConfiguration.getElementFactory(), designerBuilderFactory);
        return new LogicFlowConvertConfiguration(designerBuilderFactory, designerToDefinitionsExecutor, definitionsToJsonExecutor);
    }


    public void register(String name, Class<? extends DesignerModelBuilder> builderClass) {
        logicFlowConvertConfiguration.getDesignerBuilderFactory().register(name, builderClass);
    }


    public LogicFlowExecutor getLogicFlowExecutor() {
        return logicFlowExecutor;
    }

    public LogicFlowBuilderFactory getLogicFlowBuilderFactory() {
        return logicFlowBuilderFactory;
    }

    public LogicFlowConfiguration getLogicFlowConfiguration() {
        return logicFlowConfiguration;
    }

    public LogicFlowConvertConfiguration getLogicFlowConvertConfiguration() {
        return logicFlowConvertConfiguration;
    }

    public LogicRuntimeService getLogicRuntimeService() {
        return logicRuntimeService;
    }

    public void setLogicRuntimeService(LogicRuntimeService logicRuntimeService) {
        this.logicRuntimeService = logicRuntimeService;
    }
}
